package com.gitee.neuray.security.admin.user.web;

import com.gitee.neuray.security.admin.result.AdminObjectRestResponse;
import com.gitee.neuray.security.admin.user.dto.AdminUserDto;
import com.gitee.neuray.security.admin.user.vo.AdminUserVo;
import com.gitee.neuray.security.admin.vo.FrontUser;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import com.gitee.neuray.security.admin.user.entity.AdminStaffEntity;
import com.gitee.neuray.security.admin.user.biz.AdminStaffBiz;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 员工信息表
 *
 * @author zhoyou
 * @email ${email}
 * @date 2020-03-09 03:37:55
 */
@RestController
@RequestMapping("pmUser")
public class PmStaffController {
    @Autowired
    private AdminStaffBiz adminStaffBiz;
    @Value("${auth.user.token-header}")
    private String headerName;
    @RequestMapping(value = "/front/info", method = RequestMethod.GET)
    public AdminObjectRestResponse<FrontUser> getUserInfo(String token, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(token)) {
            token = request.getHeader(headerName);
        }
        FrontUser userInfo = adminStaffBiz.getUserInfo(token);
        return new AdminObjectRestResponse().data(userInfo).msg("").rel(true);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<AdminUserVo> get(@PathVariable String id) throws Exception{
        ObjectRestResponse<AdminUserVo> entityObjectRestResponse = new ObjectRestResponse<>();
        entityObjectRestResponse.data(adminStaffBiz.getAdminUser(id)).rel(true);
        return entityObjectRestResponse;
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<AdminStaffEntity> update(@RequestBody AdminUserDto entity) throws Exception{
        adminStaffBiz.updateUser(entity);
        return new ObjectRestResponse<AdminStaffEntity>().rel(true);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<AdminUserDto> add(@RequestBody AdminUserDto entity) throws Exception{
        adminStaffBiz.addUser(entity);
        return new ObjectRestResponse<AdminUserDto>().rel(true);
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<AdminUserDto> page(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        return adminStaffBiz.page(query);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<AdminStaffEntity> remove(@PathVariable String id) {
        Object deletingObject = adminStaffBiz.selectById(id);
        adminStaffBiz.deleteById(id);
        return new ObjectRestResponse<AdminStaffEntity>().rel(true).data(deletingObject);
    }
}
